package com.panshi.job;

import com.jfinal.kit.JsonKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.panshi.tools.MyTimeTools;
import com.panshi.tools.RandomValueTools;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class TradedynamicsJob implements Runnable {
    @Override
    public void run() {

        List<Record> symbollist = Db.find("SELECT *  from ot_app_symbol_channel  left JOIN ot_app_symbols on ot_app_symbol_channel.sid=ot_app_symbols.id where type=1;");
        //拼接品种字符串
        StringBuffer tradeSymbol = new StringBuffer("'TEST'");
        for (Record symbol : symbollist)
            tradeSymbol.append(",'" + symbol.get("symbol_name") + "'");

        System.out.println(">>SYMBOL:"+tradeSymbol);

        //获得最近的交易记录
        List<Record> mt4trade = Db.use("mt4report_real").find("select TICKET,LOGIN,SYMBOL,CMD,VOLUME,OPEN_TIME  from MT4_TRADES  where SYMBOL in (" + tradeSymbol + ")  ORDER BY OPEN_TIME desc limit 10");


        System.out.println("共有mt4记录:>>>>"+mt4trade.size());
        try {


        for (Record trade : mt4trade) {
            Record tmp = new Record();//临时载体


            int login = trade.getInt("LOGIN");//交易账户
            Date open_time = trade.getDate("OPEN_TIME");//交易时间

            //交易时间
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            open_time.setHours(open_time.getHours() + 5);
            String time_format = sdf.format(open_time);
            tmp.set("time", time_format);

            //昵称
            Record usernameRecord = Db.findFirst("select mobile from ot_app_member_mt4  LEFT JOIN ot_app_member on ot_app_member_mt4.uid=ot_app_member.id where ot_app_member_mt4.mt4_real=" + login);
            String nickname="";
            nickname=null!=usernameRecord?"[" + usernameRecord.getStr("nickname").substring(0, 3) + "****" + usernameRecord.getStr("nickname").substring(7, usernameRecord.getStr("nickname").length()) + "]": RandomValueTools.getTel();
            tmp.set("nickName",nickname);

            //获得交易方向
            int type=trade.getInt("CMD");
            tmp.set("type",type);

            //获得交易品种
            String symbol=trade.getStr("SYMBOL");
            tmp.set("symbol",symbol);
            tmp.set("create_time", MyTimeTools.getUnixTimeMills());

            Db.save("ot_app_trade_dynamics",tmp);

        }
        }catch (Exception e)
        {
            e.printStackTrace();
        }

    }
}
